<?php

require_once 'Ext/Register/Save/Abstract.php';

class Ext_Register_Save_Adapter_Db extends Ext_Register_Save_Abstract
{
    const USERNAME_ALREADY_USED              = 'usernameAlreadyUsed';
    const EMAIL_ALREADY_USED                 = 'emailAlreadyUsed';
    const UNCATEGORIZED                      = 'uncategorized';
    protected $_messageTemplates = array(
        self::USERNAME_ALREADY_USED               => "The username '%name%' is already used.",
        self::EMAIL_ALREADY_USED                  => "The email '%name%' is already used.",
        self::UNCATEGORIZED                       => "uncategorized.",
    );
    
    public function save()
    {
        $result = array(
            'code'        => Ext_Register_Save_Result::FAILURE,
            'data'        => '',
            'messages'    => array(),
        );
        
        $validater = new Ext_Validate();
        $settings = Zend_Registry::get('settings');
        if($settings['userAccountUsername']){
            $validater->addValidater(new Ext_Register_Validate_Username($this->_data->username));
            
        }
        $validater->addValidater(new Ext_Validate_Adapter_Captcha($this->_data->captcha));
        $validater->addValidater(new Ext_Register_Validate_Email($this->_data->email));
        $validater->addValidater(new Ext_Register_Validate_Password($this->_data->password, $this->_data->confirmPassword));
        if(!$validater->isValid()){
            $result['messages'] = $validater->getMessages();
            return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $settings = Zend_Registry::get('settings');
        if($settings['userAccountUsername']){
            $userAccountBaseUsernameRow = App_Model_DbTable_User_Account_Base_Username::getInstance()->getRowByUsername($this->_data->username);
            if($userAccountBaseUsernameRow){
                $result['messages'][] = $this->_messager->createMessage(self::USERNAME_ALREADY_USED, array('name'=>$this->_data->username));
                return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
            }
        }
        $userAccountBaseEmailRow = App_Model_DbTable_User_Account_Base_Email::getInstance()->getRowByEmail($this->_data->email);
        if($userAccountBaseEmailRow){
            $result['messages'][] = $this->_messager->createMessage(self::EMAIL_ALREADY_USED, array('name'=>$this->_data->email));
            return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $settings = Zend_Registry::get('settings');
        $row = array(
            'nickname'  => $this->_data->nickname,
            'roleId'    => $settings['userDefaultRoleId'],
        );
        $userId = App_Model_DbTable_User::getInstance()->addNew($row);

        $settings = Zend_Registry::get('settings');
        if($settings['userAccountUsername']){
            $row = array(
                'username'  => $this->_data->username,
                'userId'    => $userId,
            );
            App_Model_DbTable_User_Account_Base_Username::getInstance()->addNew($row);
        }
        
        $row = array(
            'email'    => $this->_data->email,
            'userId'   => $userId,
            'status'   => 1,
        );
        App_Model_DbTable_User_Account_Base_Email::getInstance()->addNew($row);

        $row = array(
            'password' => md5($this->_data->password),
            'userId'   => $userId,
        );
        App_Model_DbTable_User_Account_Base::getInstance()->addNew($row);
        
        $row = array (
            'userId'         => $userId,
            'username'       => $this->_data->username,
            'email'          => $this->_data->email,
        );
        App_Model_DbTable_User_Profile::getInstance()->addNew($row);
        
        $result['code'] = Ext_Register_Save_Result::SUCCESS;
        $result['data'] = $userId;
        return new Ext_Register_Save_Result($result['code'], $result['data'], $result['messages']);
    }
}
